package com.tan.lbs;

import java.util.List;

import com.tan.lbs.dao.NPCDAO;
import com.tan.lbs.dao.impl.NPCDAOImpl;
import com.tan.lbs.entity.Condition;
import com.tan.lbs.entity.DialogNode;
import com.tan.lbs.entity.DialogNodeOrder;
import com.tan.lbs.entity.Game;
import com.tan.lbs.entity.GameRuleManager;
import com.tan.lbs.entity.NPC;
import com.tan.lbs.util.Constants;
import com.tan.lbs.util.ResourceReader;
import com.tan.lbs.util.SQLiteDatabaseHelper;
import com.tan.lbs.util.URLs;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class NPCTalkingActivity extends Activity {

	private SharedPreferences settings;
	private String npcID;
	private String dialogNodeID;
	private String gameID;
	private TextView npcDisplayTextInTalkingTextView;
	private LinearLayout interactLayout;
	private DialogNode currentDialogNode;
	private Button talkNextBtn;
	private List<DialogNodeOrder> currentDialogNodeOrders;
	private RadioGroup branches;
	private NPCDAO npcDAO;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.npctalking);

		settings = getSharedPreferences(Constants.PREFS_NAME, 0);
		Intent intent = getIntent();
		npcID = intent.getStringExtra("npcID");
		dialogNodeID = intent.getStringExtra("startDialogNodeID");
		if (npcID != null && dialogNodeID != null) {
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("currentTalkingNPCID", npcID);
			editor.putString("currentDialogNodeID", dialogNodeID);
			editor.commit();
		}

		npcID = settings.getString("currentTalkingNPCID", "");
		dialogNodeID = settings.getString("currentDialogNodeID", "");
		gameID = settings.getString("currentGameID", "");
		
		
		npcDAO = new NPCDAOImpl(this);
		NPC npc = npcDAO.readNPC(npcID, gameID);

		((TextView) findViewById(R.id.npcNameInTalking)).setText(npc.getName());

		Bitmap figure = ResourceReader.readImage(this, URLs.gameResourceLoc
				+ gameID + "/" + npc.getFigureID());

		ImageView npcFigure = (ImageView) findViewById(R.id.npcFigure);
		npcFigure.setImageBitmap(figure);
		branches = ((RadioGroup) findViewById(R.id.radio_branches));

		// update nodes
		currentDialogNode = npcDAO.readDialogNode(dialogNodeID, gameID);
		currentDialogNodeOrders = npcDAO.retrieveAdjacentDialogNodeOrder(
				currentDialogNode.getDialogNodeID(), gameID);
		// update ui

		updateTalkingUI();

		// end update

		talkNextBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (currentDialogNode.getNodeType().equals(
						DialogNode.PROGRESS_TYPE)) {
					if (currentDialogNodeOrders.size() == 0) {
						Toast.makeText(NPCTalkingActivity.this,
								"Nothing finish after the chat", Toast.LENGTH_LONG).show();
						finish();
					} else {
						if (currentDialogNode.getInteractionType().equals(
								DialogNode.ANSWER_INTERACT)) {
							String answer = ((EditText) findViewById(R.id.answerTextView))
									.getText().toString();
							if (answer.equals(currentDialogNodeOrders.get(0)
									.getAnswer())) {
								((EditText) findViewById(R.id.answerTextView))
										.setVisibility(View.GONE);
								currentDialogNode = npcDAO.readDialogNode(
										currentDialogNodeOrders.get(0)
												.getToNodeID(), gameID);
								currentDialogNodeOrders = npcDAO
										.retrieveAdjacentDialogNodeOrder(
												currentDialogNode
														.getDialogNodeID(),
												gameID);
								updateTalkingUI();
							} else {
								Toast.makeText(NPCTalkingActivity.this,
										"Wrong answer. Please try again.", Toast.LENGTH_LONG).show();
								finish();
							}
						} else if (currentDialogNode.getInteractionType()
								.equals(DialogNode.BRANCH_INTERACT)) {
							for (int i = 0; i < currentDialogNodeOrders.size(); ++i) {
								RadioButton branch = ((RadioButton) branches
										.getChildAt(i));
								branch.setVisibility(View.GONE);
							}
							currentDialogNode = npcDAO.readDialogNode(
									currentDialogNodeOrders.get(
											branches.indexOfChild(findViewById(branches.getCheckedRadioButtonId())))
											.getToNodeID(), gameID);
							currentDialogNodeOrders = npcDAO.retrieveAdjacentDialogNodeOrder(
									currentDialogNode.getDialogNodeID(), gameID);
							updateTalkingUI();
						} else if (currentDialogNode.getInteractionType()
								.equals(DialogNode.DISPLAY_INTERACT)) {
							currentDialogNode = npcDAO.readDialogNode(
									currentDialogNodeOrders.get(0)
											.getToNodeID(), gameID);
							currentDialogNodeOrders = npcDAO.retrieveAdjacentDialogNodeOrder(
									currentDialogNode.getDialogNodeID(), gameID);
							updateTalkingUI();
						}

					}
				} else if (currentDialogNode.getNodeType().equals(
						DialogNode.FINISH_TYPE)) {
					Toast.makeText(NPCTalkingActivity.this, "You finished the chat.",
							Toast.LENGTH_LONG).show();
					finish();
				}

			}

		});
	}

	private void updateTalkingUI() {
		((Button) findViewById(R.id.moreMediaInTalking))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent();
						
						  intent.putExtra("mediaID", currentDialogNode.getContainedMediaID());
						  intent.setClass(NPCTalkingActivity.this,
								  NPCTalkingMediaActivity.class);
						  NPCTalkingActivity.this.startActivity(intent);
						 
					}
				});

		npcDisplayTextInTalkingTextView = ((TextView) findViewById(R.id.npcDisplayTextInTalking));
		npcDisplayTextInTalkingTextView.setText(currentDialogNode
				.getDisplayText());
		interactLayout = (LinearLayout) findViewById(R.id.npcTalkingInteractonLayout);

		talkNextBtn = (Button) findViewById(R.id.talkNextBtn);

		if (currentDialogNode.getNodeType().equals(DialogNode.PROGRESS_TYPE)) {
			if (currentDialogNodeOrders.size() == 0)
				talkNextBtn.setText(R.string.leaveTalk);
			else {
				talkNextBtn.setText(R.string.talkNext);
				if (currentDialogNode.getInteractionType().equals(
						DialogNode.ANSWER_INTERACT)) {
					((EditText) findViewById(R.id.answerTextView))
							.setVisibility(View.VISIBLE);
				} else if (currentDialogNode.getInteractionType().equals(
						DialogNode.BRANCH_INTERACT)) {
					for (int i = 0; i < currentDialogNodeOrders.size(); ++i) {
						DialogNodeOrder order = currentDialogNodeOrders.get(i);
						RadioButton branch = ((RadioButton) branches
								.getChildAt(i));
						branch.setText(order.getAnswer());
						branch.setVisibility(View.VISIBLE);
					}
				}

			}
		} else if (currentDialogNode.getNodeType().equals(
				DialogNode.FINISH_TYPE)) {
			talkNextBtn.setText(R.string.finishTalk);
			npcDAO.updateNPCStatus(npcID, NPC.END_CHAT);
			//update gamerule
			GameRuleManager.getInstance(this, this.getSharedPreferences(Constants.PREFS_NAME, 0).getString("currentGameID", "")).gameRuleCheck(npcID,Condition.FINISH_CHAT_CONDITION);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();

	}
}
